/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.tnmk.worktime.gui.history.filter;

import javax.swing.RowFilter;
import javax.swing.table.TableModel;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.tnmk.common.util.Filter;
import org.tnmk.worktime.gui.HistoryController;

/**
 *
 * @author tnmkhoi
 */
public class TimeRowFilter extends RowFilter{
  private static Logger log = Logger.getLogger(TimeRowFilter.class);
  private final Filter<DateTime> filter;

  public TimeRowFilter(Filter<DateTime> filter) {
    this.filter = filter;
  }
  
  @Override
  public boolean include(Entry entry) {
    Object model = entry.getModel();
    Object identifier = entry.getIdentifier();
    if (model == null) return false;
    if (!(model instanceof TableModel)) {
      log.debug("Model:"+model.getClass().getSimpleName()+":"+model);
      return false;
    }
    TableModel tableModel = (TableModel)model;
    Integer row = (Integer)identifier;
    Object inTimeObj = tableModel.getValueAt(row, HistoryController.COL_INTIME);
    DateTime inTime = (DateTime)inTimeObj;
    return filter.accept(inTime);
  }

  public Filter<DateTime> getFilter() {
    return filter;
  }
  
}
